package com.javasm.service.impl;

import com.javasm.dao.impl.BookDaoImpl;
import com.javasm.dao.impl.GameDaoImpl;
import com.javasm.entity.*;
import com.javasm.service.BookService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/21 21:33
 * @since JDK 1.8
 */
public class BookServiceImpl implements BookService {

    /**
     * 分页查询图书列表
     *
     * @param page     当前页码
     * @param pageSize 每页查询条数
     * @param book     条件查询的信息
     * @return
     */
    @Override
    public PageInfo selectBookByPage(int page, int pageSize, Book book) {

        List<Book> bookList = new BookDaoImpl().selectBookByPage(page, pageSize, book);
        int totalCount = new BookDaoImpl().selectTotalCount(book);
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotalCount(totalCount);
        pageInfo.setBookList(bookList);
        return pageInfo;


    }

    /**
     * 查询所有图书一级分类
     *
     * @return
     */
    @Override
    public List<BookTopSort> selectBookTopSort() {
        return new BookDaoImpl().selectBookTopSort();
    }

    /**
     * 根据一级图书分类id查询二级分类信息
     *
     * @param tsbId 一级分类id
     * @return
     */
    @Override
    public List<BookSecondSort> selectBookSecondSortByTsbId(String tsbId) {
        return new BookDaoImpl().selectBookSecondSortByTsbId(tsbId);
    }

    /**
     * 分页查询图书分类列表
     *
     * @param page     当前页码
     * @param pageSize 每页查询条数
     * @return
     */
    @Override
    public PageInfo selectBookSortByPage(int page, int pageSize) {
        List<Map<String, Object>> bookSortList = new BookDaoImpl().selectBookSortByPage(page, pageSize);
        List<BookSecondSort> list = new ArrayList<>(10);
        bookSortList.forEach(bookSort -> {
            BookSecondSort bookSecondSort = new BookSecondSort();
            bookSecondSort.setTsb_id((int) bookSort.get("tsb_id"));
            bookSecondSort.setTsb_name((String) bookSort.get("tsb_name"));
            bookSecondSort.setSsb_id((int) bookSort.get("ssb_id"));
            bookSecondSort.setSsb_name((String) bookSort.get("ssb_name"));
            list.add(bookSecondSort);
        });
        int totalCount = new BookDaoImpl().selectBookSortTotalCount();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotalCount(totalCount);
        pageInfo.setBookSecondSortList(list);
        return pageInfo;
    }

    /**
     * 新增图书分类信息
     *
     * @param tsb          一级分类id/name
     * @param ssb          二级分类name
     * @param tsbAddEither 控制单表插入或是多表插入的布尔值
     * @return
     */
    @Override
    public int insertBookSort(String tsb, String ssb, String tsbAddEither) {
        if ("false".equals(tsbAddEither)) { // 说明只新增二级分类
            return new BookDaoImpl().insertBookSecondSort(tsb, ssb);
        } else {
            return new BookDaoImpl().insertBookTopAndSecondSort(tsb, ssb);
        }
    }

}
